﻿Shader "Unlit/CellUpdate"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" { }
        _Weight("Weight", Range(5.01, 9.8)) = 4.8
        _Step("Step", float) = 0
    }
    SubShader
    {

        Pass
        {
            Tags { "LightMode" = "MetaCell" }
            ZWrite off
            ZTest off
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct appdata
            {
                float4 vertex: POSITION;
                float2 uv: TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex: SV_POSITION;
                float2 uv: TEXCOORD0;
            };
			//Texture2D<half> _MainTex;
            sampler2D _MainTex;
			float2 _CellTexSize;
			sampler2D _CellTexture;
            float _Weight;
            float _Step;
            v2f vert (appdata v)
            {
                v2f o = (v2f)0;
                float4 positionCS = TransformObjectToHClip(v.vertex);
                o.vertex = positionCS;
                o.uv = v.uv;
                return o;
            }

            half frag (v2f i) : SV_Target
            {
       
                float2 gap = _Step / _CellTexSize;

                float2 uv1 = i.uv + float2(-1, 0) * gap;

                float2 uv3 = i.uv + float2(0, -1) * gap;
                float2 uv4 = i.uv + float2(0, 0) * gap;
                float2 uv5 = i.uv + float2(0, 1) * gap;

                float2 uv7 = i.uv + float2(1, 0) * gap;

                

                half t1 = tex2D(_MainTex, uv1).x;
                half t3 = tex2D(_MainTex, uv3).x;
                half t4 = tex2D(_MainTex, uv4).x;
                half t5 = tex2D(_MainTex, uv5).x;
                half t7 = tex2D(_MainTex, uv7).x;

                if(t4 < 0)
                {
                    float a = -t4 * .5;
                    a = min(a, 2);
                    t4 = max(t4 + a, 0);
                    return t4;
                }

                half sum = 0;

                half weight = _Weight;

               
                sum += max(t1, 0) / weight;
      
                sum += max(t3, 0) / weight;
                sum += max(t4, 0) / weight;
                sum += max(t5, 0) / weight;
                sum += max(t7, 0) / weight;

				return sum;
            }

            ENDHLSL

        }
    }
}
